package org.jflame.commons.convert;

import org.jflame.commons.util.ArrayHelper;
import org.jflame.commons.util.StringHelper;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

public interface ValueReaderSupport {

    /**
     * 获取key对应的字符串值
     * 
     * @param key
     * @return
     */
    String getValue(String key);

    default public <T> T getValue(final String key, T defaultValue, final Converter<String,T> valueConverter) {
        String value = getValue(key);
        T result = null;
        if (StringHelper.isNotEmpty(value)) {
            result = valueConverter.convert(value.trim());
        }
        return Objects.isNull(result) ? defaultValue : result;
    }

    default public <T> T getValue(final String key, final Converter<String,T> valueConverter) {
        return getValue(key, null, valueConverter);
    }

    default public Boolean getBoolean(final String key) {
        Converter<String,Boolean> converter = ConverterRegistry.getDefaultConverter(String.class, Boolean.class)
                .get();
        return getValue(key, converter);
    }

    default public Boolean getBoolean(final String key, boolean defaultValue) {
        Converter<String,Boolean> converter = ConverterRegistry.getDefaultConverter(String.class, Boolean.class)
                .get();
        return getValue(key, defaultValue, converter);
    }

    default public <T extends Number> T getNumber(final String key, Class<T> numClass, T defaultValue) {
        Converter<String,T> converter = ConverterRegistry.getDefaultConverter(String.class, numClass)
                .get();
        return getValue(key, defaultValue, converter);
    }

    default public Long getLong(final String key) {
        return getNumber(key, Long.class, null);
    }

    default public long getLong(final String key, long defaultValue) {
        return getNumber(key, long.class, defaultValue);
    }

    default public Integer getInt(final String key) {
        return getNumber(key, Integer.class, null);
    }

    default public int getInt(final String key, int defaultValue) {
        return getNumber(key, int.class, defaultValue);
    }

    default public int[] getIntArray(final String key) {
        return getValue(key, (value) -> {
            return ArrayHelper.toIntArray(StringHelper.split(value));
        });
    }

    default public List<Integer> getIntegerList(final String key) {
        return getValue(key, (value) -> StringHelper.splitAsIntList(value, ","));
    }

    default public Double getDouble(final String key) {
        return getNumber(key, Double.class, null);
    }

    default public double getDouble(final String key, double defaultValue) {
        return getNumber(key, double.class, defaultValue);
    }

    default public Float getFloat(final String key) {
        return getNumber(key, Float.class, null);
    }

    default public float getFloat(final String key, float defaultValue) {
        return getNumber(key, float.class, defaultValue);
    }

    default public BigDecimal getBigDecimal(final String key) {
        return getBigDecimal(key, null);
    }

    default public BigDecimal getBigDecimal(final String key, BigDecimal defaultValue) {
        return getNumber(key, BigDecimal.class, defaultValue);
    }

    default public String getString(final String key) {
        return getValue(key);
    }

    default public String getString(final String key, final String defaultValue) {
        return getValue(key, defaultValue, (v) -> v);
    }

    default public String[] getStringArray(final String key) {
        return getValue(key, (value) -> StringHelper.split(value));
    }

    default public List<String> getStringList(final String key) {
        return getValue(key, (value) -> StringHelper.splitAsList(value, ","));
    }

}
